package com.zhang.topic.array;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @title: 两数之和
 * @author: zhang
 * @date: 2022/4/5 22:04
 */
public class LeetCode1 {
    public static void main(String[] args) {

        int[] nums = {4,6,13,8,7,9};
        System.out.println(Arrays.toString(twoSum(nums, 16)));
        System.out.println(Arrays.toString(new int[2]));
    }



    /**时间复杂度 O(n)*/
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> storeMap = new HashMap<>();
        int[] result = new int[2];
        for (int i = 0; i < nums.length; i++) {
            if (storeMap.containsKey(target - nums[i])) {
                result[0] = storeMap.get(target - nums[i]);
                result[1] = i;
            } else {
                storeMap.put(nums[i], i);
            }
        }
        return result;
    }
}
